package com.hns.can.safety.web;

import com.hns.can.base.entity.SysUserEntity;
import com.hns.can.common.domain.GridPage;
import com.hns.can.common.service.CommonInitParamService;
import com.hns.can.common.vo.CommonParamsVo;
import com.hns.can.common.vo.ReportSummaryVo;
import com.hns.can.common.web.BaseController;
import com.hns.can.safety.service.FaultsCountService;
import com.hns.can.safety.vo.FaultsCountsParamVo;
import com.hns.can.safety.vo.RealTimeFailureParamVo;
import com.hns.frame.enumrate.MsgType;
import com.hns.frame.exception.BusinessException;
import com.hns.frame.mybatis.Pagination;
import com.hns.tool.export.ExportUtil;
import com.hns.tool.pub.DateTool;
import com.hns.tool.pub.PubUtil;
import com.hns.tool.pub.ResponseUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;

import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 故障统计Controller
 * @author:zhangzq
 * @email:
 * @version Revision 2.0.0
 */
@Controller
@RequestMapping("/faultsCount")
public class FaultsCountController extends BaseController {

    final private String[] headerNames = {"机构", "线路", "ECU类型","故障车台", "故障总数(次)", "未修复", "已修复" };
    final private String[] headerFields = {"organName", "lineName", "ecuMftName", "summary1", "summary2", "summary3", "summary4" };

    @Autowired
    private FaultsCountService faultsCountService;
    @Autowired
    private CommonInitParamService commonInitParamService;

    /**
     * 分页查询故障统计
     * @param vo
     * @return
     * @author:zhangzq
     * @email:
     */
    @RequestMapping("/searchFaultsCount")
    @ResponseBody
    public Object searchFaultsCount(FaultsCountsParamVo vo) throws Exception {
        SysUserEntity user = this.getCurrentUser().getUser();
        commonInitParamService.initParamsLimitCar(vo, user, Arrays.asList("T_COMM_MFT_ALM"));
        Pagination page = faultsCountService.searchFaultsCount(vo);
        GridPage grid = new GridPage(page);
        return this.getReturnResult(grid);
    }

    /**
     * 查询故障统计
     * @param vo
     * @return
     * @author:zhangzq
     * @email:
     */
    @RequestMapping("/getCarFaultsCountData")
    @ResponseBody
    public Object getCarFaultsCountData(FaultsCountsParamVo vo) throws Exception {
        SysUserEntity user = this.getCurrentUser().getUser();
        commonInitParamService.initParamsLimitCar(vo, user, Arrays.asList("T_COMM_MFT_ALM"));
        List<ReportSummaryVo> list = faultsCountService.getCarFaultsCountData(vo);
        return this.getReturnResult(list);
    }

    /**
     * 查询故障统计用于图表
     * @param vo
     * @return
     * @author:zhangzq
     * @email:
     */
    @RequestMapping("/searchFaultsCountDataForChart")
    @ResponseBody
    public Object getFaultsCountDataForChart(FaultsCountsParamVo vo) throws Exception {
        SysUserEntity user = this.getCurrentUser().getUser();
        commonInitParamService.initParamsLimitCar(vo, user, Arrays.asList("T_COMM_MFT_ALM"));
        List<ReportSummaryVo> list = faultsCountService.getFaultsCountDataForChart(vo);
        return this.getReturnResult(list);
    }

    /**
     * 构造导出文档的表头
     * @param statisType
     * @param headerNames
     * @param headerFields
     * @return
     * @author:zhangzq
     * @email:
     */
    private void initFileFields(String statisType,List headerNames,List headerFields){
        if(statisType.equals("LINE")){
            headerNames.add("排名");
            headerNames.add("运营线路");
            headerNames.add("所属机构");
            headerNames.add("故障总数");
            headerNames.add("故障车辆数");
            headerNames.add("已修复(次)");
            headerNames.add("未修复(次)");
            headerFields.add("ranking");
            headerFields.add("lineName");
            headerFields.add("organName");
            headerFields.add("cnt");
            headerFields.add("carCnt");
            headerFields.add("rptcnt");
            headerFields.add("unrptcnt");
        }
        else if(statisType.equals("CAR")){
            headerNames.add("排名");
            headerNames.add("车辆");
            headerNames.add("车辆编号");
            headerNames.add("运营线路");
            headerNames.add("线路平均值");
            headerNames.add("故障总数");
            headerNames.add("已修复(次)");
            headerNames.add("未修复(次)");
            headerFields.add("ranking");
            headerFields.add("licPltNo");
            headerFields.add("carInCd");
            headerFields.add("lineName");
            headerFields.add("lineAverage");
            headerFields.add("cnt");
            headerFields.add("rptcnt");
            headerFields.add("unrptcnt");
        }
        else{
            headerNames.add("排名");
            headerNames.add("故障类型");
            headerNames.add("故障总数");
            headerNames.add("故障车辆数");
            headerNames.add("已修复(次)");
            headerNames.add("未修复(次)");
            headerFields.add("ranking");
            headerFields.add("ecuMftName");
            headerFields.add("cnt");
            headerFields.add("carCnt");
            headerFields.add("rptcnt");
            headerFields.add("unrptcnt");
        }
    }

    /**
     * 导出故障统计Excel文档
     * @param vo
     * @param response
     * @return
     * @author:zhangzq
     * @email:
     */
    @RequestMapping("/exportFaultsCountExcel")
    public void exportFaultsCountExcel(FaultsCountsParamVo vo,HttpServletResponse response) throws Exception {
        List<ReportSummaryVo> list =getFaultsCountFileData(vo);
        List headerNames=new ArrayList();
        List headerFields=new ArrayList();
        initFileFields(vo.getStatisType(),headerNames,headerFields);
        ExportUtil.exportFiles(headerNames, headerFields, list, response, "excel", URLEncoder.encode("车辆故障统计", "UTF-8") + DateTool.getCurrentSysDate("yyyyMMddHHmmss"), "车辆故障统计数据");
    }

    /**
     * 导出故障统计Pdf文档
     * @param vo
     * @param response
     * @return
     * @author:zhangzq
     * @email:
     */
    @RequestMapping("/exportFaultsCountPdf")
    public void exportFaultsCountPdf(FaultsCountsParamVo vo,HttpServletResponse response) throws Exception {
        List<ReportSummaryVo> list =getFaultsCountFileData(vo);
        List headerNames=new ArrayList();
        List headerFields=new ArrayList();
        initFileFields(vo.getStatisType(),headerNames,headerFields);
        ExportUtil.exportFiles(headerNames,headerFields, list, response, "pdf", URLEncoder.encode("车辆故障统计", "UTF-8") + DateTool.getCurrentSysDate("yyyyMMddHHmmss"), "车辆故障统计数据");
    }

    /**
     * 获取故障统计用于导出
     * @param vo
     * @return
     * @author:zhangzq
     * @email:
     */
    private List<ReportSummaryVo> getFaultsCountFileData(FaultsCountsParamVo vo) throws Exception {
        SysUserEntity user = this.getCurrentUser().getUser();
        commonInitParamService.initParamsLimitCar(vo, user, Arrays.asList("T_COMM_MFT_ALM"));
        List<ReportSummaryVo> list;
        if(vo.getStatisType().equalsIgnoreCase("car")){
            list = faultsCountService.getCarFaultsCountData(vo);
        }
        else{
            list = faultsCountService.getFaultsCount(vo);
        }
        if (PubUtil.isEmpty(list)) {
            throw new BusinessException(MsgType.WARN.getFlag(), "没有数据导出！");
        }
        return list;
    }
}
